/*
 *  nautilus-svn-extension.c - Subversion extension for Nautilus
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public
 *  License along with this library; if not, write to the Free
 *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  Author: Jack Xu Hong <jack.xu.hong@gmail.com>
 *
 */

#include <config.h>

#include <gtk/gtk.h>
#include <glade/glade.h>

#include <libgnomevfs/gnome-vfs-utils.h>

#include <libnautilus-extension/nautilus-extension-types.h>
#include <libnautilus-extension/nautilus-info-provider.h>
#include <libnautilus-extension/nautilus-column-provider.h>
#include <libnautilus-extension/nautilus-menu-provider.h>

#include "nautilus-svn-extension.h"
#include "nautilus-svn-checkout.h"

/* Icons */
#define ICON_CHECKOUT	"go-down"
#define ICON_COMMIT		"go-up"
#define ICON_UPDATE		"reload"
#define ICON_REVERT		"undo"

/* Emblems */
#define CONTROLLED		"cvs-controlled"
#define MODIFIED		"cvs-modified"
#define REMOVED			"cvs-removed"
#define ADDED			"cvs-added"
#define CONFLICT		"cvs-conflict"
#define NEW_FILE		"cvs-new-file"


struct _NautilusSvnExtensionPriv {
	GtkWidget *dialog_about;
	GtkWidget *dialog_checkout;
};

static GObjectClass *parent_class;
static NautilusSvnExtension *instance;

/*
static void
nautilus_svn_about_callback (NautilusMenuItem *item,
                             gpointer user_data)
{
	GladeXML *xml;
	GtkWidget *about_dialog;

	xml = glade_xml_new (GLADEDIR "/" "nautilus-svn-about.glade", NULL, NULL);
	about_dialog = glade_xml_get_widget(xml, "dialog_about");

	gtk_widget_show(about_dialog);
}
*/

static void
nautilus_svn_extension_checkout_cb (NautilusMenuItem *item,
                                    gchar            *dir)
{
	NautilusSvnExtensionPriv *priv;

	priv = instance->priv;

	nautilus_svn_checkout_show(NULL, dir);

	g_free (dir);
}

static NautilusOperationResult
nautilus_svn_extension_update_file_info (NautilusInfoProvider    *provider,
                                         NautilusFileInfo        *file,
                                         GClosure                *update_complete,
                                         NautilusOperationHandle **handle)
{
	return NAUTILUS_OPERATION_COMPLETE;
}

/* nautilus_column_provider_get_columns
 *
 * This function is called by Nautilus to get the column objects
 * exported by the provider.
 *
 * The function should return a GList of allocated NautilusColumn
 * items.
 */
static GList *
nautilus_svn_extension_get_columns (NautilusColumnProvider *provider)
{
	NautilusColumn *column;

	column = nautilus_column_new ("NautilusSvn::version_info_column",
				      "NautilusSvn::version_info",
				      _("Version Info"),
				      _("Information from the version control system"));

	return g_list_append (NULL, column);
}

static
GList*
nautilus_svn_extension_get_background_items (NautilusMenuProvider *provider,
                                             GtkWidget            *window,
                                             NautilusFileInfo     *current_folder)
{
	GList *items = NULL;
	NautilusMenuItem *item_checkout;
	gchar *uri;
	gchar *dir;

	item_checkout = nautilus_menu_item_new("NautilusSvn::checkout",
	                                     _("SVN Checkout"),
	                                     _("Check out source"),
	                                     ICON_CHECKOUT);

	uri = nautilus_file_info_get_uri (current_folder);
	if (uri != NULL) {
		dir = gnome_vfs_get_local_path_from_uri (uri);
		g_free (uri);
	} else {
		dir = NULL;
	}

	g_signal_connect(item_checkout,
	                 "activate",
	                 G_CALLBACK (nautilus_svn_extension_checkout_cb),
	                 dir);

	items = g_list_append(items, item_checkout);

	return items;
}

static
GList*
nautilus_svn_extension_get_file_items (NautilusMenuProvider *provider,
                                       GtkWidget            *window,
                                       GList                *files)
{
	return NULL;
}

static
GList*
nautilus_svn_extension_get_toolbar_items (NautilusMenuProvider *provider,
                                          GtkWidget            *window,
                                          NautilusFileInfo     *current_folder)
{
	return NULL;
}

static void
nautilus_svn_extension_info_provider_iface_init (NautilusInfoProviderIface *iface)
{
	iface->update_file_info = nautilus_svn_extension_update_file_info;
}

static void
nautilus_svn_extension_column_provider_iface_init (NautilusColumnProviderIface *iface)
{
	iface->get_columns = nautilus_svn_extension_get_columns;
}

static void
nautilus_svn_extension_menu_provider_iface_init (NautilusMenuProviderIface *iface)
{
	iface->get_background_items = nautilus_svn_extension_get_background_items;
	iface->get_file_items = nautilus_svn_extension_get_file_items;
	iface->get_toolbar_items = nautilus_svn_extension_get_toolbar_items;
}

static void
nautilus_svn_extension_instance_init (NautilusSvnExtension *svn)
{
	NautilusSvnExtensionPriv *priv;
	instance = svn;
	priv = g_new0 (NautilusSvnExtensionPriv, 1);
	instance->priv = priv;
}

static void
nautilus_svn_extension_class_init (NautilusSvnExtensionClass *class)
{
	parent_class = g_type_class_peek_parent (class);
}

static GType svn_type = 0;

GType
nautilus_svn_extension_get_type (void)
{
	return svn_type;
}

void
nautilus_svn_extension_register_type (GTypeModule *module)
{
	static const GTypeInfo info = {
                sizeof (NautilusSvnExtensionClass),
                (GBaseInitFunc) NULL,
                (GBaseFinalizeFunc) NULL,
                (GClassInitFunc) nautilus_svn_extension_class_init,
                NULL,
                NULL,
                sizeof (NautilusSvnExtension),
                0,
                (GInstanceInitFunc) nautilus_svn_extension_instance_init,
        };

	static const GInterfaceInfo info_provider_iface_info = {
		(GInterfaceInitFunc) nautilus_svn_extension_info_provider_iface_init,
		NULL,
		NULL
	};

	static const GInterfaceInfo column_provider_iface_info = {
		(GInterfaceInitFunc) nautilus_svn_extension_column_provider_iface_init,
		NULL,
		NULL
	};

	static const GInterfaceInfo menu_provider_iface_info = {
		(GInterfaceInitFunc) nautilus_svn_extension_menu_provider_iface_init,
		NULL,
		NULL
	};

	svn_type = g_type_module_register_type (module,
	                                        G_TYPE_OBJECT,
	                                        "NautilusSvnExtension",
	                                        &info, 0);

	g_type_module_add_interface (module,
	                             svn_type,
	                             NAUTILUS_TYPE_INFO_PROVIDER,
	                             &info_provider_iface_info);

	g_type_module_add_interface (module,
	                             svn_type,
	                             NAUTILUS_TYPE_COLUMN_PROVIDER,
	                             &column_provider_iface_info);

	g_type_module_add_interface (module,
                                 svn_type,
                                 NAUTILUS_TYPE_MENU_PROVIDER,
                                 &menu_provider_iface_info);

}

